package dwetl.model;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import java.io.Serializable;
import java.util.LinkedList;
import java.util.List;

public class DimMdfTable extends DimTable implements Serializable {
	private static final long serialVersionUID = -4614490493985759255L;
	private boolean enable_dim_cache=true;
    private List<DimTable> dtbls=new LinkedList<DimTable>();

    public DimMdfTable() {
        super();
    }
    private String projectName;

    public String getProjectName() {
        return projectName;
    }

    public void setProjectName(String projectName) {
        this.projectName = projectName;
    }

    @Override
    public String toString() {
        String rl= "DimMdfTable [table_name=" + this.getTable_name() + ", etl_columns=" + this.getEtl_columns() + "]";
        for(DimTable dt : dtbls){
            rl+="\r\n"+dt.toString();
        }
        return rl;
    }

    public List<DimTable> getDimTables(){
        return this.dtbls;
    }

    public void setDimTables(List<DimTable> dimTables){
        if(dimTables!=null) {
            this.dtbls = dimTables;
        }
    }

    public JSONObject toJson(){
        JSONObject jo=new JSONObject();
        jo.put("table_name",this.getTable_name());
        if(this.getEtl_columns()!=null && this.getEtl_columns().size()>0){
            JSONArray ja=new JSONArray();
            for(DimColumn dc:this.getEtl_columns()){
                JSONObject joj=dc.toJson();
                ja.add(joj);
            }
            jo.put("etl_columns",ja);
        }
        //unique columns
        JSONObject ukjo=new JSONObject();
        ukjo.put("if_exists_skip",this.isIf_exists_skip());
        if(this.getUnique_key_date_column()!=null){
            ukjo.put("unique_key_date_column",this.getUnique_key_date_column());
        }
        if(this.getUncheck_columns()!=null){
            JSONArray ja=new JSONArray();
            ja.addAll(this.getUncheck_columns());
            ukjo.put("uncheck_columns",ja);
        }
        if(this.getUnique_key_columns()!=null){
            JSONArray ja=new JSONArray();
            ja.addAll(this.getUnique_key_columns());
            ukjo.put("unique_key_columns",ja);
        }

        if(dtbls!=null && dtbls.size()>0){
            JSONArray ja=new JSONArray();
            for(DimTable dt:dtbls){
                JSONObject joj=dt.toJson();
                ja.add(ja);
            }
            jo.put("dim_tables",ja);
        }

        JSONObject rljo=new JSONObject();
        rljo.put("dim_mdf_table",jo);
        if(this.getProjectName()!=null){
            rljo.put("project_name",this.getProjectName());
        }
        return rljo;
    }

    public boolean isEnable_dim_cache() {
        return enable_dim_cache;
    }

    public void setEnable_dim_cache(boolean enable_dim_cache) {
        this.enable_dim_cache = enable_dim_cache;
    }
}
